﻿using MediatR;
using S.RBAC.Api.Application.Command.StaffCommand;
using S.RBAC.Domain.Model;
using S.RBAC.Infrastructure.Interface;

namespace S.RBAC.Api.Application.CommandHandlers.StaffHandler
{
    public class StaffUpdateCommandHandler : IRequestHandler<StaffUpdateCommand, int>
    {
        protected readonly IStaffRepository _staff;

        public StaffUpdateCommandHandler(IStaffRepository staff)
        {
            _staff = staff;
        }

        public async Task<int> Handle(StaffUpdateCommand request, CancellationToken cancellationToken)
        {
            var info = await _staff.GetStaffList(t => t.StaffId != request.StaffId && t.EmployeeID == request.EmployeeID);

            var list = info.FirstOrDefault();

            if (list != null)
            {
                return 0;
            }

            return await _staff.UpdStaff(new Staff
            {
                StaffId = request.StaffId,
                StaffName = request.StaffName,
                Gender = request.Gender,
                EmployeeID = request.EmployeeID,
                IDNumber = request.IDNumber,
                Phone = request.Phone,
                Photograph = request.Photograph,
                UnitName = request.UnitName,
                StaffStateId = request.StaffStateId,
                DepartmentId = request.DepartmentId,
                EntryDate = request.EntryDate,
                PostId = request.PostId,
                StationId = request.StationId,
                AccountNumber = request.AccountNumber,
                Password = request.Password,
                RoleId = request.RoleId,
                Area = request.Area,
                Location = request.Location,
                CreateBy = request.CreateBy,
                CreateDate = request.CreateDate,
                UpdateBy = "孔令琪",
                UpdateDate = DateTime.Now,
                IsDel = request.IsDel,
            });
        }
    }
}
